package com.library.borrow.service;

import com.library.book.dao.BookDao;
import com.library.book.dao.BookDaoImpl;
import com.library.book.model.Book;
import com.library.borrow.dao.BorrowRecordDao;
import com.library.borrow.dao.BorrowRecordDaoImpl;
import com.library.borrow.model.BorrowRecord;
import com.library.util.PageModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class BorrowRecordServiceImpl implements BorrowRecordService {
    private BorrowRecordDao borrowRecordDao = new BorrowRecordDaoImpl();
    private BookDao bookDao = new BookDaoImpl();

    @Override
    public boolean borrowBook(BorrowRecord borrowRecord) {
        // 检查图书是否存在且可借
        Book book = bookDao.getBookById(borrowRecord.getBookId());
        if (book == null || !"在库".equals(book.getStatus())) {
            return false;
        }
        
        // 设置借阅记录的初始值
        if (borrowRecord.getBorrowDate() == null) {
            borrowRecord.setBorrowDate(new Date());
        }
        
        // 默认借阅期限为30天
        if (borrowRecord.getReturnDate() == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(borrowRecord.getBorrowDate());
            calendar.add(Calendar.DAY_OF_MONTH, 30);
            borrowRecord.setReturnDate(calendar.getTime());
        }
        
        // 设置状态为"借出"
        borrowRecord.setStatus("借出");
        
        // 设置创建和更新时间
        Date now = new Date();
        borrowRecord.setCreatetime(now);
        borrowRecord.setUpdatetime(now);
        
        // 添加借阅记录
        int result = borrowRecordDao.addBorrowRecord(borrowRecord);
        if (result > 0) {
            // 更新图书状态为"借出"
            return bookDao.updateBookStatus(borrowRecord.getBookId(), "借出") > 0;
        }
        return false;
    }

    @Override
    public boolean returnBook(Integer id, Date actualReturnDate) {
        // 获取借阅记录
        BorrowRecord record = borrowRecordDao.getBorrowRecordById(id);
        if (record == null || !"借出".equals(record.getStatus()) && !"逾期".equals(record.getStatus())) {
            return false;
        }
        
        // 设置实际归还日期
        if (actualReturnDate == null) {
            actualReturnDate = new Date();
        }
        
        // 更新借阅记录
        int result = borrowRecordDao.returnBook(id, actualReturnDate);
        if (result > 0) {
            // 更新图书状态为"在库"
            return bookDao.updateBookStatus(record.getBookId(), "在库") > 0;
        }
        return false;
    }

    @Override
    public boolean deleteBorrowRecord(Integer id) {
        return borrowRecordDao.deleteBorrowRecordById(id) > 0;
    }

    @Override
    public boolean updateBorrowRecord(BorrowRecord borrowRecord) {
        // 设置更新时间
        borrowRecord.setUpdatetime(new Date());
        return borrowRecordDao.updateBorrowRecord(borrowRecord) > 0;
    }

    @Override
    public BorrowRecord getBorrowRecordById(Integer id) {
        return borrowRecordDao.getBorrowRecordById(id);
    }

    @Override
    public List<Map<String, Object>> getAllBorrowRecords() {
        return borrowRecordDao.getAllBorrowRecords();
    }

    @Override
    public List<Map<String, Object>> getBorrowRecordsByBookId(Integer bookId) {
        return borrowRecordDao.getBorrowRecordsByBookId(bookId);
    }

    @Override
    public List<Map<String, Object>> getBorrowRecordsByReaderName(String readerName) {
        return borrowRecordDao.getBorrowRecordsByReaderName(readerName);
    }

    @Override
    public List<Map<String, Object>> getBorrowRecordsByStatus(String status) {
        return borrowRecordDao.getBorrowRecordsByStatus(status);
    }

    @Override
    public void checkAndUpdateOverdueStatus() {
        // 获取所有借出状态的记录
        List<Map<String, Object>> borrowedRecords = borrowRecordDao.getBorrowRecordsByStatus("借出");
        Date today = new Date();
        
        for (Map<String, Object> recordMap : borrowedRecords) {
            Date returnDate = (Date) recordMap.get("return_date");
            Integer recordId = (Integer) recordMap.get("id");
            
            // 如果应还日期已过，则更新状态为逾期
            if (returnDate != null && returnDate.before(today)) {
                borrowRecordDao.updateBorrowRecordStatus(recordId, "逾期");
            }
        }
    }

    @Override
    public List<Map<String, Object>> getComingDueBorrowRecords(int days) {
        // 获取所有借出状态的记录
        List<Map<String, Object>> borrowedRecords = borrowRecordDao.getBorrowRecordsByStatus("借出");
        List<Map<String, Object>> comingDueRecords = new ArrayList<>();
        
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        Date futureDate = calendar.getTime();
        
        for (Map<String, Object> recordMap : borrowedRecords) {
            Date returnDate = (Date) recordMap.get("return_date");
            
            // 如果应还日期在指定天数内，则添加到结果列表
            if (returnDate != null && !returnDate.before(today) && !returnDate.after(futureDate)) {
                comingDueRecords.add(recordMap);
            }
        }
        
        return comingDueRecords;
    }
    
    @Override
    public List<Map<String, Object>> getBorrowRecordsByPage(PageModel pageModel) {
        // 首先获取符合条件的总记录数
        int recordCount = borrowRecordDao.getBorrowRecordsCount(pageModel);
        pageModel.setRecordCount(recordCount);
        
        // 然后进行分页查询
        return borrowRecordDao.getBorrowRecordsByPage(pageModel);
    }
} 